package com.sanji.sjmallmodule.phoneshell;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.BoolRes;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.sanji.sjmallapp.BaseActivity;
import com.sanji.sjmallapp.MallApplication;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallconfig.ConfigAPI;
import com.sanji.sjmallconfig.ConfigConts;
import com.sanji.sjmallmanager.http.NoHttpCallBack;
import com.sanji.sjmallmanager.http.NoHttpListener;
import com.sanji.sjmallmodule.goods.SearchTypeBean;
import com.sanji.sjmallutils.Tools;
import com.sanji.sjmallwidget.MyGrideView;
import com.sanji.sjmallwidget.XListView;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/7.
 */

public class PhoneShellActivity extends BaseActivity implements View.OnClickListener, NoHttpListener {
    @BindView(R.id.common_left_tv)
    TextView titleTv;
    @BindView(R.id.phone_shell_select)
    LinearLayout filterLayout;
    @BindView(R.id.phone_shell_select_arrow)
    ImageView arrowImg;
    @BindView(R.id.phone_shell_model_tv)
    TextView modelTv;
    @BindView(R.id.phone_shell_material_quality_tv)
    TextView materialQualityTv;
    @BindView(R.id.phone_shell_technics_tv)
    TextView technicsTv;
    @BindView(R.id.phone_shell_list)
    XListView listView;
    @BindView(R.id.pop_shell_brand_grid_view)
    MyGrideView brandGridView;//品牌
    @BindView(R.id.pop_shell_model_grid_view)
    MyGrideView modelGridView;//机型

    private PhoneShellAdapter shellAdapter;
    private View rootView;
    private PopupWindow filterPop;
    private int popHeight;
    String brandUrl = "";

    private Request<JSONObject> request;
    int paramId = 1;//品牌参数默认为1，参数中的id
    private List<PhoneShellBrandBean> brandList;
    private List<PhoneShellBrandBean> modeList;

    private PhoneShellBrandAdapter brandAdapter;
    private PhoneShellBrandAdapter modelAdapter;

    private String modeParam = "sc_categoryNum=";//机型
    Boolean isAll = false;//品牌的全部


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_phone_shell);
        ButterKnife.bind(this);
        popHeight = MallApplication.myShare.getInt(ConfigConts.ScreenHight, 800) * 3 / 5;
        initView();
    }

    private void initView() {
        titleTv.setText("小千定制");
        filterLayout.setOnClickListener(this);
        listView.setPullLoadEnable(false);
        listView.setPullRefreshEnable(false);
        brandList = new ArrayList<>();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.phone_shell_select:
                shellFilterPopWindow();
                break;
        }
    }

    /**
     * 条件筛选
     */
    public void shellFilterPopWindow() {
        Tools.backgroundAlpaha(this, 0.6f);
        if (filterPop == null) {
            View view = LayoutInflater.from(this).inflate(R.layout.pop_shell_filter, null);
            filterPop = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, popHeight);
            brandGridView = (MyGrideView) view.findViewById(R.id.pop_shell_brand_grid_view);
            modelGridView = (MyGrideView) view.findViewById(R.id.pop_shell_model_grid_view);
            filterPop.setBackgroundDrawable(new ColorDrawable());
            filterPop.setFocusable(true);
            filterPop.setOutsideTouchable(true);
            filterPop.setAnimationStyle(R.style.my_popwindow_left_anim_style);
            filterPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    Tools.backgroundAlpaha(PhoneShellActivity.this, 1.0f);
                }
            });
            //请求品牌数据
            requestBranadData(paramId);
        }
        filterPop.showAsDropDown(filterLayout);


    }

    /**
     * 网络请求品牌类型
     */
    public void requestBranadData(int id) {
        brandUrl = ConfigAPI.PHONE_SHELL_FILTER + id;
        request = NoHttp.createJsonObjectRequest(brandUrl, RequestMethod.GET);
        request.setCancelSign(brandUrl.hashCode());
        NoHttpCallBack.getInstance().add(this, brandUrl.hashCode(), request, this, true, true);
    }

    @Override
    public void onSuccessed(int what, Response response) {
        int requestCode = response.responseCode();
        if (requestCode == 200) {
            if (what == brandUrl.hashCode()) {
                JSONArray jsonArray = (JSONArray) response.get();
                List<PhoneShellBrandBean> mData = new ArrayList<>();
                if (jsonArray.length() == 0) {
                    Tools.toastTools(this, "暂时还没有数据");
                } else {
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject object = jsonArray.optJSONObject(i);
                        int id = object.optInt("id");
                        String name = object.optString("name");
                        String categoryNum = object.optString("categoryNum");
                        PhoneShellBrandBean brandBean = new PhoneShellBrandBean(categoryNum, id, name);
                        mData.add(brandBean);
                    }
                    if (isAll) {
                        brandList.addAll(mData);
                    } else {
                        modeList.addAll(mData);
                    }

                }
                if (isAll) {
                    if (null == brandAdapter) {
                        brandAdapter = new PhoneShellBrandAdapter(this, brandList);
                        brandGridView.setAdapter(brandAdapter);
                    } else {
                        brandAdapter.notifyDataSetChanged();
                    }
                    onItemClick(brandGridView, brandAdapter);
                } else {
                    if (null == modelAdapter) {
                        modelAdapter = new PhoneShellBrandAdapter(this, modeList);
                        modelGridView.setAdapter(modelAdapter);
                    } else {
                        modelAdapter.notifyDataSetChanged();
                    }
                    onItemClick(modelGridView, modelAdapter);
                }

            }
        } else {
            Tools.toastTools(this, response.getException().toString());
        }
    }

    @Override
    public void onFailed(int what, Response response) {
        Tools.toastTools(this, "连接超时！");
    }

    public void onItemClick(final MyGrideView grideView, final PhoneShellBrandAdapter adapter) {
        grideView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setSelection(position);
                PhoneShellBrandBean bean = (PhoneShellBrandBean) adapter.getItem(position);
                switch (grideView.getId()) {
                    case R.id.pop_shell_brand_grid_view:
                        //更改参数
                        if (position != 0) {
                            //控制机型的显示与隐藏
                            modelGridView.setVisibility(View.VISIBLE);
                            isAll = false;
                            //请求机型
                            paramId = bean.getId();
                            requestBranadData(paramId);
                        } else {
                            modelGridView.setVisibility(View.GONE);
                            isAll = true;
                            paramId = 1;
                        }
                        break;
                }
            }
        });
    }

}
